package facelets;

import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.HashMap;
import java.util.Map;

import javax.faces.FacesException;

import com.sun.facelets.tag.TagConfig;
import com.sun.facelets.tag.TagHandler;
import com.sun.facelets.tag.TagLibrary;

/**
 * Library for JSTL Functions
 * 
 * @author Jacob Hookom
 * @version $Id: JstlFnLibrary.java,v 1.2 2005/08/24 04:38:59 jhook Exp $
 */
public class YedraFnLibrary implements TagLibrary {

    public final static String Namespace = "http://www.xplayat.es/ysf/functions";
    
    private final Map<String, Method> fns = new HashMap<String, Method>();
    
    public YedraFnLibrary() {
        super();
        try {
            Method[] methods = YedraFunction.class.getMethods();
            for (int i = 0; i < methods.length; i++) {
                if (Modifier.isStatic(methods[i].getModifiers())) {
                    fns.put(methods[i].getName(), methods[i]);
                }
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public boolean containsNamespace(String ns) {
        return Namespace.equals(ns);
    }

    public boolean containsTagHandler(String ns, String localName) {
        return false;
    }

    public TagHandler createTagHandler(String ns, String localName,
            TagConfig tag) throws FacesException {
        return null;
    }

    public boolean containsFunction(String ns, String name) {
        if (Namespace.equals(ns)) {
            return this.fns.containsKey(name);
        }
        return false;
    }

    public Method createFunction(String ns, String name) {
        if (Namespace.equals(ns)) {
            return (Method) this.fns.get(name);
        }
        return null;
    }
    
}
